// index.js
// 获取应用实例
const app = getApp();
const { HTTP } = require('../../utils/http');
const { api } = require('../../utils/api');
const { request } = new HTTP();

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    loginShow: !app.globalData.isLogin, // 先写死，后面根据登录状态来判断

    reList: [] ,   // 推荐列表
    reIndex: 0,
    dailyArr: [],
    wishArr: []

  },

  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
  },
  onShow() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0    // 根据tab的索引值设置
      }) 
    }

    app.userInfoCallBack = () => {
      this._userRecommend();
    };

    app.loginCallBack = () => {
      // 判断是否要登录
      if (app.globalData.isLogin) {
        this.setData({
          loginShow: true
        });
      } else {
        this.setData({
          loginShow: false
        });
      }
      if (app.globalData.userInfo) {
        this.setData({
          userInfo: app.globalData.userInfo,
          hasUserInfo: true
        })
      } 
    };
  },
  async _getUserInfo(cb) {
    const {returnCode = '', bean = {}} = await request({
      url: api.getUser,
      method: 'get',
      data: {}
    });
    if (returnCode === '0000') {
      app.globalData.userInfo = bean.user;
      cb && cb();
    }
  },

  
  // 不登录
  noLogin(){
    this.setData({
      loginShow: false
    })
  },
  selectP(opts) {
    // '是否喜欢：0-无感，1-喜欢，2-超喜欢', 
    console.log('----opts--')
    console.log(opts.target.dataset.type)
    this._userRecommendType(opts.target.dataset.type)
  },
  // 解密获取appid 成功
  loginSuccess() {
    this.setData({ loginShow: false });
    this._bindPhone();
  },
  closeLogin() {
    this.setData({ loginShow: false });
  },
  onClickLeft() {
    console.log('点击了左边的按钮')
    wx.navigateTo({
      url: "/pages/preference/preference"
    })
  },

  // 绑定用户手机号 （先用微信获取的手机号，后期根据需要添加自定义手机号弹框）
  async _bindPhone() {
    const { returnCode = '', bean = '' } = await request({
      url: api.bindPhone,
      data: {
        phone: app.globalData.phoneNumber,
        openid: app.globalData.openid
      },
      method: 'post'
    });
    if (returnCode === '0000') {
      app.globalData.isLogin = false;
      this.setData({
        loginShow: false
      })
      wx.setStorageSync('token', bean);
      wx.navigateTo({url: '../perfectInformation/index'});
    }
  },
  // 获取首页推荐用户信息
  async _userRecommend() {
    const { returnCode = '', beans = '' } = await request({
      url: api.userRecommend,
      data: { },
      method: 'get'
    });
    if (returnCode === '0000') {
      this.setData({
        reList: [...beans, ...beans, ...beans],
        dailyArr: beans[this.data.reIndex].daily.split('|~|~'),
        wishArr: beans[this.data.reIndex].wish.split('|~|~'),
      })
    }
  },

  // 超喜欢、喜欢、不喜欢
  async _userRecommendType(type) {
    let objT = {
      0: '不喜欢',
      1: '喜欢',
      2: '超喜欢',
    }
    let objT2 = {
      0: 'cannel',
      1: 'success',
      2: 'success',
    }
    const { returnCode = '', beans = '' } = await request({
      url: `/api/user/recommend/type/${type}`,
      data: { },
      method: 'get'
    });
    if (returnCode === '0000') {
      wx.showToast({
        title: objT[type],
        duration: 2000,
        icon: objT2[type],
        success: (res) => {
          this.setData({
            reIndex: this.data.reIndex + 1,
          })
          this.setData({
            dailyArr: this.data.reList[this.data.reIndex].daily.split('|~|~'),
            wishArr: this.data.reList[this.data.reIndex].wish.split('|~|~'),
          })
        },
        fail: (res) => {},
        complete: (res) => {},
      })
    }
  },
  
})
